<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          clearable
          size="small"
          placeholder="请输入数据源名称"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation show="" :permission="permission" />
    </div>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="700px"
    >
      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="已办管理名称" prop="name">
          <el-input v-model="form.name" style="width: 500px" />
        </el-form-item>
        <el-form-item label="获取方式" prop="queryType">
          <el-radio-group v-model="form.queryType" size="mini" style="width: 500px" @change="changeType">
            <el-radio-button label="1">数据库查询</el-radio-button>
            <el-radio-button label="2">API接口查询</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="form.queryType.toString() == '2'" label="api地址" prop="apiUrl">
          <el-input v-model="form.apiUrl" style="width: 500px" />
        </el-form-item>
        <el-form-item v-show="form.queryType.toString() == '2'" label="请求方式" prop="requestMethod">
          <el-select v-model="form.requestMethod" placeholder="请选择请求方式" style="width: 180px">
            <el-option
              v-for="item in dict.request_method"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-show="form.queryType.toString() == '2'" label="请求类型" prop="contentType">
          <el-select v-model="form.contentType" placeholder="请选择请求类型" style="width: 180px">
            <el-option
              v-for="item in dict.content_type"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-show="form.queryType.toString() == '1'" label="请选择数据源" prop="dataSource">
          <el-select v-model="form.dataSource" placeholder="请选择数据源">
            <el-option label="默认数据源" value="" />
            <el-option
              v-for="item in dataSources"
              :key="item.id"
              :label="item.name"
              :value="item.dataCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-show="form.queryType.toString() == '1'" label="查询sql" prop="querySql">
          <el-input v-model="form.querySql" style="width: 500px" />
        </el-form-item>
        <el-form-item label="标题字段" prop="titleLabel">
          <el-input v-model="form.titleLabel" style="width: 180px" />
        </el-form-item>
        <el-form-item label="已办地址字段" prop="blUrlLabel">
          <el-input v-model="form.blUrlLabel" style="width: 180px" />
        </el-form-item>
        <el-form-item label="申请人姓名字段" prop="sqrLabel">
          <el-input v-model="form.sqrLabel" style="width: 180px" />
        </el-form-item>
        <el-form-item label="申请人账号字段" prop="sqlUserIdLabel">
          <el-input v-model="form.sqlUserIdLabel" style="width: 180px" />
        </el-form-item>
        <el-form-item label="办理人名称字段" prop="blrLabel">
          <el-input v-model="form.blrLabel" style="width: 180px" />
        </el-form-item>
        <el-form-item label="办理人账号字段" prop="blrUserIdLabel">
          <el-input v-model="form.blrUserIdLabel" style="width: 180px" />
        </el-form-item>
        <el-form-item label="当前步骤字段" prop="dqbzLabel">
          <el-input v-model="form.dqbzLabel" style="width: 180px" />
        </el-form-item>
        <el-form-item label="申请时间字段" prop="sqsjLabel">
          <el-input v-model="form.sqsjLabel" style="width: 180px" />
        </el-form-item>
        <el-form-item label="当前状态字段" prop="dqztLabel">
          <el-input v-model="form.dqztLabel" style="width: 180px" />
        </el-form-item>
        <!--是否开启消息推送-->
        <!-- <el-form-item label="是否开启消息推送" prop="isSendMessage">
           <el-input v-model="form.isSendMessage" style="width: 180px"/>
         </el-form-item>-->
        <el-form-item label="状态">
          <el-radio-group v-model="form.isEnable" :disabled="form.id === user.id">
            <el-radio
              v-for="item in dict.user_status"
              :key="item.id"
              :label="item.value"
            >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%;"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column :selectable="checkboxT" type="selection" width="55" />
      <el-table-column :show-overflow-tooltip="true" prop="name" label="已办名称" />
      <el-table-column label="状态" align="center" prop="isEnable">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
            :disabled="user.id === scope.row.id"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            @change="changeEnabled(scope.row, scope.row.isEnable)"
          />
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="createTime" width="135" label="创建日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-permission="['admin','datasource:edit','datasource:del']"
        label="操作"
        width="115"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            :disabled-dle="scope.row.id === user.id"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import uyb from '@/api/system/uyb'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import crudDataSource from '@/api/system/dataSource'

const defaultForm = {
  id: null, name: null, queryType: 1, apiUrl: null, requestMethod: null, contentType: null, querySql: null,
  titleLabel: null, blUrlLabel: null, sqrLabel: null, sqlUserIdLabel: null, blrLabel: null, blrUserIdLabel: null,
  dqbzLabel: null, sqsjLabel: null, dqztLabel: null, isSendMessage: 'false', isEnable: 'true'
}
export default {
  name: 'ManagerYb',
  components: { crudOperation, rrOperation, udOperation, pagination },
  cruds() {
    return CRUD({ title: '已办配置管理', url: 'rest/api/manager/yb', crudMethod: { ...uyb }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  // 数据字典
  dicts: ['user_status', 'content_type', 'request_method'],
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  watch: {
    changeValue: {
      handler(newName, oldName) { // 值发生变化时
        if (newName === 1) { // 数据库查询
          this.rules.querySql = [{ required: true, message: '请输入查询sql', trigger: 'blur' }]
          this.rules.apiUrl = []
          this.rules.requestMethod = []
          this.rules.contentType = []
        } else if (newName === 2) {
          this.rules.querySql = []
          this.rules.apiUrl = [{ required: true, message: '请输入api地址', trigger: 'blur' }]
          this.rules.requestMethod = [{ required: true, message: '请选择请求方式', trigger: 'change' }]
          this.rules.contentType = [{ required: true, message: '请选择请求类型', trigger: 'change' }]
        } else {
          this.rules.querySql = []
          this.rules.apiUrl = []
          this.rules.requestMethod = []
          this.rules.contentType = []
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    crudDataSource.queryEnable().then(res => {
      this.dataSources = res
    }).catch(() => {
      console.log('查询数据源信息失败！')
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'managerDb:add'],
        edit: ['admin', 'managerDb:edit'],
        del: ['admin', 'managerDb:del']
      },
      rules: {
        name: [{ required: true, message: '请输入已办名称', trigger: 'blur' }],
        queryType: [{ required: true, message: '请选择数据获取方式', trigger: 'change' }],
        titleLabel: [{ required: true, message: '请输入已办标题字段名称', trigger: 'blur' }],
        blUrlLabel: [{ required: true, message: '请输入办理地址字段名称', trigger: 'blur' }],
        sqrLabel: [{ required: true, message: '请输入已办字段名称', trigger: 'blur' }],
        blrUserIdLabel: [{ required: true, message: '请输入办理人字段名称', trigger: 'blur' }],
        sqlUserIdLabel: [{ required: true, message: '请输入申请人字段名称', trigger: 'blur' }],
        tjsjLabel: [{ required: true, message: '请输入提交时间字段名称', trigger: 'blur' }]
      },
      changeValue: 1,
      dataSources: []
    }
  },
  methods: {
    [CRUD.HOOK.afterAddError](crud) {
      this.afterErrorMethod(crud)
    },
    [CRUD.HOOK.afterEditError](crud) {
      this.afterErrorMethod(crud)
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      form.isEnable = form.isEnable.toString()
    },
    checkboxT(row, rowIndex) {
      return row.id !== this.user.id
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.user_status[val] + '" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        uyb.edit(data).then(res => {
          this.crud.notify(this.dict.label.user_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.enabled = !data.enabled
        })
      }).catch(() => {
        data.enabled = !data.enabled
      })
    },
    changeType(events) {
      this.changeValue = events
    }
  }
}
</script>

<style scoped>

</style>
